import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


boston = tf.contrib.learn.datasets.load_dataset('boston')

# print(boston.data[:,5])
# print(boston.data.shape)
# print(boston.target.shape)



x_train = boston.data[:,5]
y_train = boston.target


x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)

w = tf.Variable(0.)
b = tf.Variable(0.)

y_hat = w * x +b

loss = tf.reduce_mean(tf.square(y - y_hat))

train = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for aerp in range(1000):
        for i in range(len(y_train)):
            # print(x_train[i],y_train[i])
            # msg = np.array([y_train[i]])
            sess.run(train,feed_dict={x:x_train[i],y:y_train[i]})

        print(sess.run([w,b],feed_dict={x:x_train[i],y:y_train[i]}))
    b_value, w_value = sess.run([b, w])


y_hat = w_value * x_train + b_value

# print(y_hat)
plt.plot(x_train, y_train, 'bo', label='Real Data')
plt.plot(x_train, y_hat, 'r', label='Predicted Data')
plt.legend()
plt.show()

